<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
/*****
  * This class extends CodeIgniter's default Validation class
  * @author     Michael Wales
  * @email      webmaster@michaelwales.com
  * @filename   MY_Validation.php
  * @title      Extended Validation Library
  * @url        http://www.michaelwales.com/
  * @version    1.0
  *****/

class MY_Validation extends CI_Validation {


    function MY_Validation() {
        parent::CI_Validation();
    }

    // This function overwrites CodeIgniter's default set_fields() function, it allows you to omit the $fields aray
    // and just use the keys from the $rules array. If you pass an array to this function it will use a combination of
    // the passed array and fill in the blanks with the $rules array.
    function set_fields($data = '', $field = '', $separator = '_') {    
        if ($data == '') {
            if (count($this->_fields) == 0 && count($this->_rules) == 0) {
                return FALSE;
            }
        } else {
            if ( ! is_array($data)) {
                $data = array($data => $field);
            }
            
            if (count($data) > 0) {
                $this->_fields = $data;
            }
        }
        
        foreach($this->_rules as $key => $val) {                  
            $text = ucwords(str_replace($separator, ' ', $key));             
            $auto_fields[$key] = $text;     
        }
        
        $this->_fields = array_merge($auto_fields, $this->_fields);
        
        foreach($this->_fields as $key => $val) {        
            $this->$key = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
            
            $error = $key.'_error';
            if ( ! isset($this->$error)) {
                $this->$error = '';
            }
        }        
    }
}